package com.xiaoyu.lanling.feature.verify.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.verify.GetFullVerifyTokenEvent
import com.xiaoyu.lanling.event.verify.VerifyResultEvent
import com.xiaoyu.lanling.event.verify.GetVerifyTokenEvent
import com.xiaoyu.lanling.event.verify.VerifyConfirmEvent

object VerifyData {

    fun getFaceVerifyToken(requestTag: Any) {
        RequestCreator.create<GetVerifyTokenEvent>(requestTag, GetVerifyTokenEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_GET_VERIFY_TOKEN)
                .enqueue()
    }

    fun getFullVerifyToken(requestTag: Any) {
        RequestCreator.create<GetFullVerifyTokenEvent>(requestTag, GetFullVerifyTokenEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_GET_FULL_TOKEN)
                .enqueue()
    }

    fun verifyResult(requestTag: Any) {
        RequestCreator.create<VerifyResultEvent>(requestTag, VerifyResultEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_VERIFY_RESULT)
                .enqueue()
    }

    fun verifyConfirm(requestTag: Any) {
        RequestCreator.create<VerifyConfirmEvent>(requestTag, VerifyConfirmEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_VERIFY_CONFIRM)
                .enqueue()
    }

    fun verifyConfirm(requestTag: Any, productId: String) {
        RequestCreator.create<VerifyConfirmEvent>(VerifyConfirmEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_VERIFY_CONFIRM)
                .setRequestHandler(object : RequestDefaultHandler<VerifyConfirmEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): VerifyConfirmEvent {
                        val jsonData = originData.optJson("data")
                        jsonData.put("productId", productId)
                        return VerifyConfirmEvent(requestTag, jsonData)
                    }

                    override fun onRequestFinish(event: VerifyConfirmEvent) {
                        super.onRequestFinish(event)
                        event.post()
                    }
                })
                .enqueue()
    }
}